package com.tensquare.articlecrawler.task;
/*
 *ClassName:ArticleTask
 *Description:文章任务类
 *@author:可爱的大鸭子
 */

import com.tensquare.articlecrawler.pipeline.ArticleDbPipeline;
import com.tensquare.articlecrawler.pipeline.ArticleTxtPipeline;
import com.tensquare.articlecrawler.processor.ArticleProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.scheduler.RedisScheduler;

@Component
public class ArticleTask {
    @Autowired
    private ArticleDbPipeline articleDbPipeline;
    @Autowired
    private RedisScheduler redisScheduler;
    @Autowired
    private ArticleProcessor articleProcessor;
    @Autowired
    private ArticleTxtPipeline articleTxtPipeline;

    /* 爬取ai数据*/
    @Scheduled(cron="00 09 20 * * ?")
    public void aiTask() {
        System.out.println("爬取AI文章");
        Spider spider = Spider.create(articleProcessor);
        spider.addUrl("https://blog.csdn.net/nav/ai");
        articleDbPipeline.setChannelId("ai");
        articleTxtPipeline.setChannelId("ai");
        spider.addPipeline(articleDbPipeline);
        spider.addPipeline(articleTxtPipeline);
        spider.setScheduler(redisScheduler);
        spider.start();
        //spider.stop();
    }

    /**
     * 爬取web数据
     */
/*    @Scheduled(cron="0 35 16 * * ?")
    public void dbTask(){
        System.out.println("爬取前端文章");
        Spider spider = Spider.create(articleProcessor);
        spider.addUrl("https://blog.csdn.net/nav/web");
        articleDbPipeline.setChannelId("web");
        articleTxtPipeline.setChannelId("web");
        spider.addPipeline(articleDbPipeline);
        spider.addPipeline(articleTxtPipeline);
        spider.setScheduler(redisScheduler);
        spider.start();
        spider.stop();
    }*/

    /**
     * 爬取数据库数据
     */
/*    @Scheduled(cron="00 36 16 * * ?")
    public void webTask(){
        System.out.println("爬取数据库文章");
        Spider spider = Spider.create(new ArticleProcessor());
        spider.addUrl("https://blog.csdn.net/nav/db");
        articleDbPipeline.setChannelId("db");
        articleTxtPipeline.setChannelId("db");
        spider.addPipeline(articleDbPipeline);
        spider.addPipeline(articleTxtPipeline);
        spider.setScheduler(redisScheduler);
        spider.start();
        spider.stop();
    }*/
}
